<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HEAD response (Server) 💡</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast">
<link rel="up" href="../more_examples.html" title="HTTP Examples">
<link rel="prev" href="head_request_client.html" title="HEAD request (Client) 💡">
<link rel="next" href="http_relay.html" title="HTTP Relay 💡">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="head_request_client.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../more_examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="http_relay.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="beast.more_examples.head_response_server"></a><a class="link" href="head_response_server.html" title="HEAD response (Server) 💡">HEAD response
      (Server) 💡</a>
</h3></div></div></div>
<p>
        When a server receives a <a href="https://tools.ietf.org/html/rfc7231#section-4.3.2" target="_top">HEAD
        request</a>, the response should contain the entire header that would
        be delivered if the method was GET, except that the body is omitted.
      </p>
<pre class="programlisting"><span class="comment">/** Handle a HEAD request for a resource.
*/</span>
<span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">class</span> <span class="identifier">SyncStream</span><span class="special">,</span>
    <span class="keyword">class</span> <span class="identifier">DynamicBuffer</span>
<span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">do_server_head</span><span class="special">(</span>
    <span class="identifier">SyncStream</span><span class="special">&amp;</span> <span class="identifier">stream</span><span class="special">,</span>
    <span class="identifier">DynamicBuffer</span><span class="special">&amp;</span> <span class="identifier">buffer</span><span class="special">,</span>
    <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">is_sync_stream</span><span class="special">&lt;</span><span class="identifier">SyncStream</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">,</span>
        <span class="string">"SyncStream requirements not met"</span><span class="special">);</span>
    <span class="keyword">static_assert</span><span class="special">(</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">is_dynamic_buffer</span><span class="special">&lt;</span><span class="identifier">DynamicBuffer</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">,</span>
        <span class="string">"DynamicBuffer requirements not met"</span><span class="special">);</span>

    <span class="comment">// We deliver this payload for all GET requests</span>
    <span class="keyword">static</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span> <span class="identifier">payload</span> <span class="special">=</span> <span class="string">"Hello, world!"</span><span class="special">;</span>

    <span class="comment">// Read the request</span>
    <span class="identifier">request</span><span class="special">&lt;</span><span class="identifier">string_body</span><span class="special">&gt;</span> <span class="identifier">req</span><span class="special">;</span>
    <span class="identifier">read</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">buffer</span><span class="special">,</span> <span class="identifier">req</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span>
    <span class="keyword">if</span><span class="special">(</span><span class="identifier">ec</span><span class="special">)</span>
        <span class="keyword">return</span><span class="special">;</span>

    <span class="comment">// Set up the response, starting with the common fields</span>
    <span class="identifier">response</span><span class="special">&lt;</span><span class="identifier">string_body</span><span class="special">&gt;</span> <span class="identifier">res</span><span class="special">;</span>
    <span class="identifier">res</span><span class="special">.</span><span class="identifier">version</span><span class="special">(</span><span class="number">11</span><span class="special">);</span>
    <span class="identifier">res</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">server</span><span class="special">,</span> <span class="string">"test"</span><span class="special">);</span>

    <span class="comment">// Now handle request-specific fields</span>
    <span class="keyword">switch</span><span class="special">(</span><span class="identifier">req</span><span class="special">.</span><span class="identifier">method</span><span class="special">())</span>
    <span class="special">{</span>
    <span class="keyword">case</span> <span class="identifier">verb</span><span class="special">::</span><span class="identifier">head</span><span class="special">:</span>
    <span class="keyword">case</span> <span class="identifier">verb</span><span class="special">::</span><span class="identifier">get</span><span class="special">:</span>
    <span class="special">{</span>
        <span class="comment">// A HEAD request is handled by delivering the same</span>
        <span class="comment">// set of headers that would be sent for a GET request,</span>
        <span class="comment">// including the Content-Length, except for the body.</span>
        <span class="identifier">res</span><span class="special">.</span><span class="identifier">result</span><span class="special">(</span><span class="identifier">status</span><span class="special">::</span><span class="identifier">ok</span><span class="special">);</span>
        <span class="identifier">res</span><span class="special">.</span><span class="identifier">content_length</span><span class="special">(</span><span class="identifier">payload</span><span class="special">.</span><span class="identifier">size</span><span class="special">());</span>

        <span class="comment">// For GET requests, we include the body</span>
        <span class="keyword">if</span><span class="special">(</span><span class="identifier">req</span><span class="special">.</span><span class="identifier">method</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">verb</span><span class="special">::</span><span class="identifier">get</span><span class="special">)</span>
        <span class="special">{</span>
            <span class="comment">// We deliver the same payload for GET requests</span>
            <span class="comment">// regardless of the target. A real server might</span>
            <span class="comment">// deliver a file based on the target.</span>
            <span class="identifier">res</span><span class="special">.</span><span class="identifier">body</span><span class="special">()</span> <span class="special">=</span> <span class="identifier">payload</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="keyword">break</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">default</span><span class="special">:</span>
    <span class="special">{</span>
        <span class="comment">// We return responses indicating an error if</span>
        <span class="comment">// we do not recognize the request method.</span>
        <span class="identifier">res</span><span class="special">.</span><span class="identifier">result</span><span class="special">(</span><span class="identifier">status</span><span class="special">::</span><span class="identifier">bad_request</span><span class="special">);</span>
        <span class="identifier">res</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="identifier">field</span><span class="special">::</span><span class="identifier">content_type</span><span class="special">,</span> <span class="string">"text/plain"</span><span class="special">);</span>
        <span class="identifier">res</span><span class="special">.</span><span class="identifier">body</span><span class="special">()</span> <span class="special">=</span> <span class="string">"Invalid request-method '"</span> <span class="special">+</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">req</span><span class="special">.</span><span class="identifier">method_string</span><span class="special">())</span> <span class="special">+</span> <span class="string">"'"</span><span class="special">;</span>
        <span class="identifier">res</span><span class="special">.</span><span class="identifier">prepare_payload</span><span class="special">();</span>
        <span class="keyword">break</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="special">}</span>

    <span class="comment">// Send the response</span>
    <span class="identifier">write</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">res</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span>
    <span class="keyword">if</span><span class="special">(</span><span class="identifier">ec</span><span class="special">)</span>
        <span class="keyword">return</span><span class="special">;</span>
<span class="special">}</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
      Falco<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="head_request_client.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../more_examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="http_relay.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
